#!/opt/homebrew/bin/python3.13
import functools

def a_decorator(func):
    def wrapper():
        print("我是装饰器")
        func()
        print("我是装饰器")
    return wrapper
def hello():
    print("hello")


@a_decorator
def hello2():
    print("hello2")


decoratorx = a_decorator(hello)
decoratorx()

print("\n")
hello2()
print("\n")



def a_decorator2(func):
    def wrapper(msg):
        print("我是装饰器")
        func(msg)
        print("我是装饰器")
    return wrapper

@a_decorator2
def hell3(msg):
    print("msg={}".format(msg))

hell3("hello3")
print("\n")


def a_decorator3(func):
    def wrapper(*args, **kwargs):
        print("我是装饰器")
        func(*args, **kwargs)
        print("我是装饰器")
    return wrapper

@a_decorator3
def hell4(name,age):
    print("name={},age={}".format(name, age))

hell4("tom", 25)
print("\n")


def repeatx(num):
    def my_decorator3(func):

        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            for i in range(num):
                print("我是装饰器开始")
                func(*args, **kwargs)
                print("我是装饰器结束")
        return wrapper
    return my_decorator3

@repeatx(3)
def hell5(name,age):
    print("name={},age={}".format(name, age))

print("\n")
hell5("beck", "tt")


class Count:
    def __init__(self, func):
        self.func = func
        self.__num_call = 0
    
    def __call__(self, *args, **kwds):
        self.__num_call +=  1
        print('num of calls is: {}'.format(self.__num_call))
        result = self.func(*args, **kwds)
        print('num of calls end !')
        return result

@repeatx(1) 
@Count
def helloX1(name=""):
    print("hello {}".format(name))
print("\n")
helloX1()
helloX1("tom")
helloX1("tim")

print("\n")

c1 = Count(hell3)
print(type(c1))
print("\n")





